//import { fileURLToPath, URL } from 'node:url'
import { visualizer } from 'rollup-plugin-visualizer';
import viteCompression from 'vite-plugin-compression';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import { resolve } from 'path';
import versionUpdatePlugin from './src/utils/versionUpdatePlugin';
// https://vitejs.dev/config/
const CurrentTimeVersion = new Date().getTime();
export default defineConfig({
  server: {
    port: 4100
  },
  define: {
    __APP_VERSION__: CurrentTimeVersion
  },
  plugins: [
    vue(),
    visualizer({ open: true }),
    viteCompression({
      verbose: true,
      disable: false,
      threshold: 10240,
      algorithm: 'gzip',
      ext: '.gz'
    }),
    versionUpdatePlugin({
      version: CurrentTimeVersion
    })
  ],
  resolve: {
    alias: {
      //'@': fileURLToPath(new URL('./src', import.meta.url))
      '@': resolve(__dirname, './src'),
      '*': resolve('')
    }
  },
  css: {
    preprocessorOptions: {
      scss: {
        // 两种方式都可以
        additionalData: '@import "@/assets/css/global.scss";' //resolve(__dirname, 'src/renderer/src/assets/css/global.scss')
        // additionalData: '@use "@/assets/scss/global.scss" as *;'
      }
    }
  },
  build: {
    rollupOptions: {
      output: {
        chunkFileNames: 'js/[name]-[hash].js', // 引入文件名的名称
        entryFileNames: 'js/[name]-[hash].js', // 包的入口文件名称
        assetFileNames: '[ext]/[name]-[hash].[ext]', // 资源文件像 字体，图片等
        // 最小化拆分包
        manualChunks(id) {
          if (id.includes('node_modules')) {
            return id.toString().split('node_modules/')[1].split('/')[0].toString();
          }
        }
      }
    }
  }
});
